package com.cj.bus.common.entity;


import com.cj.bus.common.enums.ErrorCodeEnum;
import com.cj.bus.common.ex.BusinessException;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * The type Response helper.
 * 统一返回相应参数
 *
 * @author yanghao
 * @date 2018 -12-21 11:36:46
 */
@Slf4j
public final class ResponseHelper {

    /**
     * Instantiates a new Response helper.
     * 无参构造方法
     *
     * @author yanghao
     * @date 2018 -12-21 13:50:03
     */
    private ResponseHelper() {
    }

    /**
     * 500错误 - 无参
     *
     * @param <T> the type parameter
     * @return the response model
     * @author yanghao
     * @date 2018 -12-21 13:50:03
     * @method Internal  server error response model.
     */
    public static <T> ResponseEntity<T> failServerError() {
        ResponseEntity response = new ResponseEntity();
        response.setStatus(Boolean.FALSE);
        response.setCode(ErrorCodeEnum.SYS_ERROR.getCode());
        response.setMessage(ErrorCodeEnum.SYS_ERROR.getDesc());
        return response;
    }

    /**
     * 500错误 - 有参
     *
     * @param <T>     the type parameter
     * @param message the message
     * @return the response model
     * @author yanghao
     * @date 2018 -12-21 13:50:03
     * @method Internal  server error response model.
     */
    public static <T> ResponseEntity<T> failServerError(String message) {
        ResponseEntity response = new ResponseEntity();
        response.setStatus(Boolean.FALSE);
        response.setCode(ErrorCodeEnum.SYS_ERROR.getCode());
        response.setMessage(message);
        return response;
    }

    /** 
    * @Description: 带自定义编码的异常错误
    * @Param: [code, message] 
    * @return: com.ctl.component.common.dto.ResponseModel<T> 
    * @Author: yjr
    * @Date: 2019/1/12 
    */ 
    public static <T> ResponseEntity<T> failServerError(Integer code,String message) {
        ResponseEntity response = new ResponseEntity();
        response.setStatus(Boolean.FALSE);
        response.setCode(null != code ? code : ErrorCodeEnum.SYS_ERROR.getCode());
        response.setMessage(message);
        return response;
    }


    /**
     * 未授权错误
     *
     * @param <T> the type parameter
     * @return the response model
     * @author yanghao
     * @date 2018 -12-21 13:50:04
     * @method Failure  response model.
     */
    public static <T> ResponseEntity<T> failUnauthorized() {
        ResponseEntity response = new ResponseEntity();
        response.setStatus(Boolean.FALSE);
        response.setCode(ErrorCodeEnum.FORBIDDEN.getCode());
        response.setMessage(ErrorCodeEnum.FORBIDDEN.getDesc());
        return response;
    }

    /**
     * 未授权错误
     *
     * @param <T> the type parameter
     * @return the response model
     * @author yanghao
     * @date 2018 -12-21 13:50:04
     * @method Failure  response model.
     */
    public static <T> ResponseEntity<T> failUnauthorized(String message) {
        ResponseEntity response = new ResponseEntity();
        response.setStatus(Boolean.FALSE);
        response.setCode(ErrorCodeEnum.FORBIDDEN.getCode());
        response.setMessage(message);
        return response;
    }

    /**
     * 成功返回
     *
     * @param <T>    the type parameter
     * @param result the result
     * @return the response model
     * @author yanghao
     * @date 2018 -12-21 13:50:04
     * @method Success  response model.
     */
    public static <T> ResponseEntity<T> success(T result) {
        ResponseEntity response = new ResponseEntity();
        response.setStatus(Boolean.TRUE);
        response.setCode(ErrorCodeEnum.SUCCESS.getCode());
        response.setMessage(ErrorCodeEnum.SUCCESS.getDesc());
        response.setResult(result);
        return response;
    }

    public static <T> ResponseEntity<T> success(T result,String message) {
        ResponseEntity response = new ResponseEntity();
        response.setStatus(Boolean.TRUE);
        response.setCode(ErrorCodeEnum.SUCCESS.getCode());
        response.setMessage(message);
        response.setResult(result);
        return response;
    }

    /**
     * 无参成功返回
     *
     * @param <T> the type parameter
     * @return the response model
     * @author yanghao
     * @date 2018 -12-21 13:50:04
     * @method Success  response model.
     */
    public static <T> ResponseEntity<T> success() {
        ResponseEntity response = new ResponseEntity();
        response.setStatus(Boolean.TRUE);
        response.setCode(ErrorCodeEnum.SUCCESS.getCode());
        response.setMessage(ErrorCodeEnum.SUCCESS.getDesc());
        response.setResult(null);
        return response;
    }

    /**
     * 成功返回，带业务码
     *
     * @param <T> the type parameter
     * @return the response model
     * @author yanghao
     * @date 2018 -12-21 13:50:04
     * @method Success  response model.
     */
    public static <T> ResponseEntity<T> success(Integer code,T result) {
        ResponseEntity response = new ResponseEntity();
        response.setStatus(Boolean.TRUE);
        response.setCode(code);
        response.setMessage(ErrorCodeEnum.SUCCESS.getDesc());
        response.setResult(result);
        return response;
    }

    /**
     * 成功返回，带业务码和错误信息
     *
     * @param <T> the type parameter
     * @return the response model
     * @author yanghao
     * @date 2018 -12-21 13:50:04
     * @method Success  response model.
     */
    public static <T> ResponseEntity<T> success(Integer code, String message) {
        ResponseEntity response = new ResponseEntity();
        response.setStatus(Boolean.TRUE);
        response.setCode(code);
        response.setMessage(message);
        response.setResult(null);
        return response;
    }

    /**
    * @Description: 统一处理 Response status 为false情况
    * @Param: [responseModel]
    * @return: void
    * @Author: yjr
    * @Date: 2019/2/25
    */
    public static void checkFailResponse(ResponseEntity  responseModel){
        if(ObjectUtil.isNull(responseModel)){
            return;
        }

        if(responseModel.getStatus()==false){
            log.error(responseModel.toString());
            throw new BusinessException(responseModel.getCode(),responseModel.getMessage());
        }
    }


    /**
     * @Description: 统一处理 Response status 为false情况
     * @Param: [responseModel,rewriteMessage]
     * @return: void
     * @Author: yjr
     * @Date: 2019/2/25
     */
    public static void checkFailResponse(ResponseEntity  responseModel,Integer rewriteCode,String  rewriteMessage ){
        if(ObjectUtil.isNull(responseModel)){
            return;
        }
        if(responseModel.getStatus()==false){
            log.error(responseModel.toString());
            throw new BusinessException(ObjectUtil.isNull(rewriteCode)?responseModel.getCode():rewriteCode,
                    ObjectUtil.isNull(rewriteMessage)?responseModel.getMessage():rewriteMessage);
        }
    }
}
